Completed
Push — master ( 3b0e28...451c77 )
by Alejandro
14:49 queued 05:57
created

MenuLayout.js ➔ componentDidUpdate   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.1852

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
ccs 1
cts 3
cp 0.3333
rs 10
c 0
b 0
f 0
cc 2
crap 3.1852
1
import React, { useState, useEffect } from 'react';
2
import { Route, Switch } from 'react-router-dom';
3
import { Swipeable } from 'react-swipeable';
4
import { faBars as burgerIcon } from '@fortawesome/free-solid-svg-icons';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import classNames from 'classnames';
7
import * as PropTypes from 'prop-types';
8
import { serverType } from '../servers/prop-types';
9
import MutedMessage from '../utils/MutedMessage';
10
import NotFound from './NotFound';
11
import './MenuLayout.scss';
12
13
const propTypes = {
14
  match: PropTypes.object,
15
  selectServer: PropTypes.func,
16
  location: PropTypes.object,
17
  selectedServer: serverType,
18
};
19
20
const MenuLayout = (TagsList, ShortUrls, AsideMenu, CreateShortUrl, ShortUrlVisits) => {
21
  const MenuLayoutComp = ({ match, location, selectedServer, selectServer }) => {
22
    const [ showSideBar, setShowSidebar ] = useState(false);
23
24
    useEffect(() => {
25
      const { params: { serverId } } = match;
26
27
      selectServer(serverId);
28
    }, []);
29
    useEffect(() => setShowSidebar(false), [ location ]);
30
31 2
    if (!selectedServer) {
32
      return <MutedMessage loading />;
33
    }
34
35
    const { params: { serverId } } = match;
36
    const burgerClasses = classNames('menu-layout__burger-icon', {
37
      'menu-layout__burger-icon--active': showSideBar,
38
    });
39
    const swipeMenuIfNoModalExists = (showSideBar) => () => {
40 2
      if (document.querySelector('.modal')) {
41
        return;
42
      }
43
44
      setShowSidebar(showSideBar);
45
    };
46
47
    return (
48
      <React.Fragment>
49
        <FontAwesomeIcon
50
          icon={burgerIcon}
51
          className={burgerClasses}
52
          onClick={() => setShowSidebar(!showSideBar)}
53
        />
54
55
        <Swipeable
56
          delta={40}
57
          className="menu-layout__swipeable"
58
          onSwipedLeft={swipeMenuIfNoModalExists(false)}
59
          onSwipedRight={swipeMenuIfNoModalExists(true)}
60
        >
61
          <div className="row menu-layout__swipeable-inner">
62
            <AsideMenu className="col-lg-2 col-md-3" selectedServer={selectedServer} showOnMobile={showSideBar} />
63
            <div className="col-lg-10 offset-lg-2 col-md-9 offset-md-3" onClick={() => setShowSidebar(false)}>
64
              <Switch>
65
                <Route exact path="/server/:serverId/list-short-urls/:page" component={ShortUrls} />
66
                <Route exact path="/server/:serverId/create-short-url" component={CreateShortUrl} />
67
                <Route exact path="/server/:serverId/short-code/:shortCode/visits" component={ShortUrlVisits} />
68
                <Route exact path="/server/:serverId/manage-tags" component={TagsList} />
69
                <Route
70
                  render={() => <NotFound to={`/server/${serverId}/list-short-urls/1`} btnText="List short URLs" />}
71
                />
72
              </Switch>
73
            </div>
74
          </div>
75
        </Swipeable>
76
      </React.Fragment>
77
    );
78
  };
79
80
  MenuLayoutComp.propTypes = propTypes;
81
82
  return MenuLayoutComp;
83
};
84
85
export default MenuLayout;
86